<template>
    <div class="forum-list" style="padding: 20,20;">
      <h2 style="text-align: center;">我的申请</h2>
      <el-table :data="postList" style="width: 100%;" :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}">
        <el-table-column prop="title" label="申请标题"></el-table-column>
        <el-table-column prop="time" label="申请时间"></el-table-column>
        <el-table-column prop="status" label="申请状态"></el-table-column>
        <el-table-column prop="id" label="链接">
          <template slot-scope="scope">
            <router-link :to="{ name: 'EveDetail', params: { id: scope.row.id } }">
              查看详情
            </router-link>
          </template>
        </el-table-column>
        <el-table-column label="操作"> 
            <template slot-scope="scope">
               <el-button type="success" @click="handleEdit(scope.row)" style="margin-right: 10px;">编辑 <i class="el-icon-edit"></i></el-button>
                <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="handleDelete(scope.row.id)"
              >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
            </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align:center; margin-top:20px; ">
      </el-pagination>
  

      <el-dialog title="申请信息" :visible.sync="dialogFormVisible" width="30%" >
        <el-form label-width="80px" size="small">
          <el-form-item label="申请名称">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="申请描述">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="附件上传">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
           multiple
           accept=".jpg,.png,.doc,.docx,.xlsx,.xls,.pptx,.txt"
          :data="{ fileName: fileName}"
          :on-success="handleSuccess"
          :show-file-list="true"
          :before-upload="beforeAvatarUpload"
        >
          <el-button type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png/doc/pdf/excel/txt文件，且不超过2MB
          </div>
        </el-upload>
      </el-form-item> 
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">保存</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>


    </div>
  </template>
  <script>
  export default {
    data() {
      return {
        postList: [], // 申请列表
        currentPage: 1, // 当前页码
        pageSize: 10, // 每页显示的数据量
        total: 0,// 数据总条数
        dialogFormVisible: false,
        form:{},
        fileName: '',
        attachmentUrl: [],
        uploadUrl: 'http://localhost:8086/upload' // 上传文件的接口地址
      }
    },
    mounted() {
      if(!this.$store.getters.getUser.id){
        this.$message.error("请登录后再进行查看！");
      }else{
      this.getpostList()
      }
    },
    methods: {
      // 获取申请列表
      getpostList() {
        // 调用后端接口获取数据
        console.log(this.$store.getters.getUser.id)
        this.request.get('/event/id', {
          params: {
            page: this.currentPage,
            size: this.pageSize,
            user_id : this.$store.getters.getUser.id
          }
        }).then((response) => {
          const data = response.data
          console.log(data)
          this.postList = data.data.list
          this.total = data.data.total
        }).catch(error => {
          console.log(error)
        })
      },
      // 每页显示数据量改变时触发
      handleSizeChange(size) {
        this.pageSize = size
        this.getpostList()
      },
      // 当前页码改变时触发
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage
        this.getpostList()
      },
      handleDelete(id){
        this.request.post("/event/delete/" + id )
        .then((response) => {
          this.$message.success("删除成功");
          this.getpostList();
        }).catch(error => {
          console.log(error)
         })
       },
       handleEdit(row) {
        this.form = row
        this.dialogFormVisible = true
      },
      save() {
        this.request.post("/event/accept", 
        {
          id: this.form.id,
          title: this.form.title,
          content: this.form.content,
          attachment: this.attachmentUrl,
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success(" 修改成功 ")
            this.dialogFormVisible = false
            // this.load()
          } else {
            this.$message.error(" 修改失败 ")
          }
        })
      },
      beforeAvatarUpload(file) {
        //alert(file.type)
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传文件大小不能超过 2MB!');
        }
        return isLt2M;
      },
      handleSuccess(response) {
        this.$message.success('上传成功');
        console.log("存储路径：" + response.virtualPath);
        console.log("文件名：" + response.fileName)
        //this.fileName = response.fileName;
        this.attachmentUrl += response.fileName;
      },
    }
  }
  </script>